package cn.iocoder.yudao.module.carbon.service.accountboundary;

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.carbon.api.accountboundary.dto.AccountBoundaryListReqDTO;
import cn.iocoder.yudao.module.carbon.controller.admin.accountboundary.vo.AccountBoundaryCreateReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.accountboundary.vo.AccountBoundaryExportReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.accountboundary.vo.AccountBoundaryPageReqVO;
import cn.iocoder.yudao.module.carbon.controller.admin.accountboundary.vo.AccountBoundaryUpdateReqVO;
import cn.iocoder.yudao.module.carbon.convert.accountboundary.AccountBoundaryConvert;
import cn.iocoder.yudao.module.carbon.dal.dataobject.accountboundary.AccountBoundaryDO;
import cn.iocoder.yudao.module.carbon.dal.mysql.accountboundary.AccountBoundaryMapper;
import cn.iocoder.yudao.module.carbon.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

/**
 * 核算边界 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class AccountBoundaryServiceImpl implements AccountBoundaryService {

    @Resource
    private AccountBoundaryMapper accountBoundaryMapper;

    @Override
    public Long createAccountBoundary(AccountBoundaryCreateReqVO createReqVO) {
        // 插入
        AccountBoundaryDO accountBoundary = AccountBoundaryConvert.INSTANCE.convert(createReqVO);
        accountBoundaryMapper.insert(accountBoundary);
        // 返回
        return accountBoundary.getId();
    }

    @Override
    public void updateAccountBoundary(AccountBoundaryUpdateReqVO updateReqVO) {
        // 校验存在
        validateAccountBoundaryExists(updateReqVO.getId());
        // 更新
        AccountBoundaryDO updateObj = AccountBoundaryConvert.INSTANCE.convert(updateReqVO);
        accountBoundaryMapper.updateById(updateObj);
    }

    @Override
    public void deleteAccountBoundary(Long id) {
        // 校验存在
        validateAccountBoundaryExists(id);
        // 删除
        accountBoundaryMapper.deleteById(id);
    }

    private void validateAccountBoundaryExists(Long id) {
        if (accountBoundaryMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.ACCOUNT_BOUNDARY_NOT_EXISTS);
        }
    }

    @Override
    public AccountBoundaryDO getAccountBoundary(Long id) {
        return accountBoundaryMapper.selectById(id);
    }

    @Override
    public List<AccountBoundaryDO> getAccountBoundaryList(Collection<Long> ids) {
        return accountBoundaryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AccountBoundaryDO> getAccountBoundaryPage(AccountBoundaryPageReqVO pageReqVO) {
        return accountBoundaryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AccountBoundaryDO> getAccountBoundaryList(AccountBoundaryExportReqVO exportReqVO) {
        return accountBoundaryMapper.selectList(exportReqVO);
    }

    @Override
    public List<AccountBoundaryDO> getAccountBoundaryList(AccountBoundaryListReqDTO reqDTO) {
        return accountBoundaryMapper.selectList(reqDTO);
    }

    @Override
    public AccountBoundaryDO getAccountBoundaryByCategory(String standard, String classification, String category) {
        return accountBoundaryMapper.selectOneByCategory(standard, classification, category);
    }
}
